package com.rwdesign.springboot.Waimai.controller;

import com.rwdesign.springboot.Waimai.entity.Shop;
import com.rwdesign.springboot.Waimai.service.ShopService;
import com.rwdesign.springboot.Waimai.service.UserService;
import com.rwdesign.springboot.Waimai.utils.ResultBody;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.List;
import java.util.Map;


/**
 * @author cdp
 * @create 2022-01-29 13:06
 */


@CrossOrigin
@RestController
@RequestMapping("/waimai/sys")
public class ShopController {

    @Autowired
    private ShopService shopService;

    @Autowired
    private UserService userService;

    public boolean isLogin(String userToken) {
        if (userToken == null) {
            return false;
        } else if (userService.selectIdByToken(userToken) != null) {
            return true;
        } else {
            return false;
        }
    }

    // 查询是否为商户
    public boolean isShoper(String userToken) {
        if (userToken == null) {
            return false;
        } else if ("1".equals(userService.selectIsShoperByUserId(userService.selectIdByToken(userToken)))) {
            return true;
        } else {
            return false;
        }
    }

    /**
     * @return
     * @Author chendanping
     * @Description //TODO 查询商铺分类列表
     * @Date 20:59 2022/3/1
     * @Param
     **/
    @RequestMapping(value = "/getAllShopTypes", method = RequestMethod.GET)
    public ResultBody getAllShopTypes(HttpServletRequest request) {
        if (!isLogin(request.getHeader("Authorization"))) {
            return ResultBody.isError(402, "Token错误或已过期");
        }
        System.out.println(request.getHeader("Authorization"));

        List<String> list = shopService.selectAllShopTypes();

        if (list != null) {
            Map data = new HashMap();
            data.put("typeList", list);
            return ResultBody.isOk(200, "success", data);
        } else {
            return ResultBody.isError(500, "error");
        }

    }


    /**
     * @return
     * @Author chendanping
     * @Description //TODO 查询商铺
     * @Date 20:59 2022/3/1
     * @Param
     **/
    @RequestMapping(value = "/getAllShops", method = RequestMethod.POST)
    public ResultBody getAllShops(@RequestBody(required = false) Map<String, String> requestInfo, HttpServletRequest request) {
        if (!isLogin(request.getHeader("Authorization"))) {
            return ResultBody.isError(402, "Token错误或已过期");
        }

        System.out.println(request.getHeader("Authorization"));

        List<Shop> list = shopService.selectAllShops(requestInfo.get("shopType"));

        if (list != null) {
            Map data = new HashMap();
            data.put("data", list);
            return ResultBody.isOk(200, "success", data);
        } else {
            return ResultBody.isError(500, "error");
        }

    }

    //商户权限

    /**
     * @return
     * @Author chendanping
     * @Description //TODO 新增商铺
     * @Date 20:59 2022/3/1
     * @Param
     **/
    @RequestMapping(value = "/createShop", method = RequestMethod.POST)
    public ResultBody createShop(@RequestBody(required = false) Map<String, String> requestInfo, HttpServletRequest request) {
        if (!isLogin(request.getHeader("Authorization"))) {
            return ResultBody.isError(402, "Token错误或已过期");
        } else if (!isShoper(request.getHeader("Authorization"))) {
            return ResultBody.isError(401, "非商家，身份验证失败");
        } else if (userService.selectShopIdByUserId(userService.selectIdByToken(request.getHeader("Authorization"))) != null) {
            return ResultBody.isError(255, "重复开店");
        } else {
            System.out.println(request.getHeader("Authorization"));

            Shop shop = new Shop();
            shop.setShopId(null);
            shop.setShopAddress(requestInfo.get("shopAddress"));
            shop.setShopName(requestInfo.get("shopName"));
            shop.setShopType(requestInfo.get("shopType"));
            shop.setShopInfo(requestInfo.get("shopInfo"));
            shop.setShopPicture(requestInfo.get("shopPicture"));
            shopService.createShop(shop);

            Integer shopId = null;
            List<Shop> list = shopService.selectAllShops(null);
            shopId = list.get(0).getShopId();
            Integer integer = userService.updateShopIdByUserId(shopId, userService.selectIdByToken(request.getHeader("Authorization")));

            if (integer == 1) {
                Map data = new HashMap();
                return ResultBody.isOk(200, "success", data);
            } else {
                return ResultBody.isError(500, "error");
            }
        }

    }


    /**
     * @return
     * @Author chendanping
     * @Description //TODO 编辑商铺信息
     * @Date 20:59 2022/3/1
     * @Param
     **/
    @RequestMapping(value = "/editShop", method = RequestMethod.POST)
    public ResultBody editShop(@RequestBody(required = false) Map<String, String> requestInfo, HttpServletRequest request) {
        if (!isLogin(request.getHeader("Authorization"))) {
            return ResultBody.isError(402, "Token错误或已过期");
        } else if (!isShoper(request.getHeader("Authorization"))) {
            return ResultBody.isError(401, "非商家，身份验证失败");
        } else if (userService.selectShopIdByUserId(userService.selectIdByToken(request.getHeader("Authorization"))) == null) {
            return ResultBody.isError(255, "暂未开店");
        } else {
            System.out.println(request.getHeader("Authorization"));

            Shop shop = new Shop();
            shop.setShopId(userService.selectShopIdByUserId(userService.selectIdByToken(request.getHeader("Authorization"))));
            shop.setShopAddress(requestInfo.get("shopAddress"));
            shop.setShopName(requestInfo.get("shopName"));
            shop.setShopType(requestInfo.get("shopType"));
            shop.setShopInfo(requestInfo.get("shopInfo"));
            shop.setShopPicture(requestInfo.get("shopPicture"));

            // 更新商铺信息
            Integer integer = shopService.updateShopByShopId(shop);


            if (integer == 1) {
                Map data = new HashMap();
                return ResultBody.isOk(200, "success", data);
            } else {
                return ResultBody.isError(500, "error");
            }
        }

    }


    /**
     * @return
     * @Author chendanping
     * @Description //TODO 查询商户的商铺
     * @Date 20:59 2022/3/1
     * @Param
     **/
    @RequestMapping(value = "/getShop", method = RequestMethod.POST)
    public ResultBody getShop(@RequestBody(required = false) Map<String, String> requestInfo, HttpServletRequest request) {
        System.out.println(request.getHeader("Authorization"));

        if (!isLogin(request.getHeader("Authorization"))) {
            return ResultBody.isError(402, "Token错误或已过期");
        } else if (!isShoper(request.getHeader("Authorization"))) {
            return ResultBody.isError(401, "非商家，身份验证失败");
        } else if (userService.selectShopIdByUserId(userService.selectIdByToken(request.getHeader("Authorization"))) == null) {
            return ResultBody.isError(255, "暂未开店");
        } else {


            List<Shop> list = shopService.selectShopByShopId(userService.selectShopIdByUserId(userService.selectIdByToken(request.getHeader("Authorization"))));

            if (list != null) {
                Map data = new HashMap();
                data.put("data", list);
                return ResultBody.isOk(200, "success", data);
            } else {
                return ResultBody.isError(500, "error");
            }
        }

    }

}
